package com.msmall.community.album.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

import com.msmall.base.util.Constants;

public final class AlbumUtil {

	/**
	 * 分页显示一个相册下的相片 pagesize
	 */
	public static final int ALBUM_PHOTO_LIST_PAGESIZE_DEFAULT=12;
	
	public static final String PREFIX_WML = "file/wml/user/";
	public static final String PREFIX_HTML = "file/html/user/";
	public static final String ALBUM_PATH = "/album/";
	
	public static String[] saveImage(File file, String filename, Long userid, String uat){
		File target = new File(getAbsoluteHomePath(userid, uat)+filename);
		if (target.exists()) {
			target.delete();
		}else{
			File tFolder = new File(getAbsoluteHomePath(userid, uat));
			if(!tFolder.exists()){
				tFolder.mkdirs();
			}
		}
		try {
			IOUtils.copyLarge(new FileInputStream(file), new FileOutputStream(target));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String[]{getHomePath(userid, uat), filename};
	}
	
	public static String getAbsoluteHomePath(Long sightid, String uat){
		return Constants.ROOTPATH + getHomePath(sightid, uat);
	}
	
	/**
	 * 生成图片路径 file/html/user/id/album/
	 * @return
	 */
	public static String getHomePath(Long userid, String uat){
		String path = "";
		if(uat.equals(Constants.USER_AGENT_TYPE_HTML)){
			path = PREFIX_HTML;
		}
		if(uat.equals(Constants.USER_AGENT_TYPE_WML)){
			path = PREFIX_WML;
		}
		path = path + userid + ALBUM_PATH;
		return path;
	}
}
